import {
  dynastyList
} from "../../../utils/commondata.js";

import {
  request
} from "../../../utils/request.js";
import regeneratorRuntime from '../../../utils/runtime';

var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainColor:appInst.globalData.mainColor,
    curIndex:0,
    dynastyList:dynastyList(),
    authorList:[],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let dynasty = this.data.dynastyList[0].name;
    this.loadAuthorList(1,dynasty);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page + 1;
    let dynasty = this.data.dynastyList[this.data.curIndex].name;
    this.loadAuthorList(page,dynasty);
  },
  handleSelectDynasty(e){
    let {index} = e.currentTarget.dataset;
    this.setData({
      curIndex:index,
      authorList:[],
    });
    let dynasty = this.data.dynastyList[index].name;
    this.loadAuthorList(1,dynasty);
  },
  /**
   * 
   * @param {} e 
   */
  handleDetail(e){
    let {id} = e.currentTarget.dataset;
    console.log(id);
    wx.navigateTo({
      url:'/pages/author/detail/index?authorId=' + id,
    });
  },

  /**
   * 查询作者
   */
  async loadAuthorList(pageNo,dynasty){
    const res = await request({
      url: "/author/pageQuery?limit=10&dynasty=" + dynasty + "&page=" + pageNo,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    this.setData({
      page:pageNo,
      authorList:[...this.data.authorList,...res.data.list],
    })

  }

})